package com.ayoka.ui.course.detail;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ayoka.R;
import com.ayoka.adapter.CourseQuestionAdapter;
import com.ayoka.bean.CourseBean;
import com.ayoka.mvp.CourseQuestionMVP.CourseQuestionModel.CourseQuestionBean;
import com.ayoka.mvp.CourseQuestionMVP.CourseQuestionPresenter;
import com.ayoka.mvp.CourseQuestionMVP.ICourseQuestionView;
import com.jcp.baselibrary.brvah.layoutmanager.MyLinearLayoutManager;
import com.jcp.baselibrary.dialog.BaseDialogBuilder;
import com.jcp.baselibrary.dialog.BaseDialogFragment;
import com.jcp.baselibrary.utils.UIUtils;

import java.util.List;

/**
 * Created by jinchunping on 2019/1/23.
 */

public class CourseDetailQuestionDialog extends BaseDialogFragment implements ICourseQuestionView{

    public static CourseDetailQuestionDialog newInstance(Builder builder) {
        CourseDetailQuestionDialog dialog = new CourseDetailQuestionDialog();
        Bundle bundle = getArgumentBundle(builder);
        bundle.putSerializable("CourseBean", builder.courseBean);
        dialog.setArguments(bundle);
        return dialog;
    }

    private CourseQuestionPresenter questionPresenter;

    private CourseBean courseBean;

    private RecyclerView recyclerView;
    private CourseQuestionAdapter courseQuestionAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        courseBean = (CourseBean) getArguments().getSerializable("CourseBean");
        questionPresenter = new CourseQuestionPresenter();
        questionPresenter.attach(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (questionPresenter != null && courseBean != null){
            questionPresenter.getCourseQuestionListData(courseBean.ID);
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected void convertView(@Nullable View contentView, BaseDialogFragment dialogFragment) {
        recyclerView = contentView.findViewById(R.id.recyclerView_question);
        recyclerView.setLayoutManager(new MyLinearLayoutManager(getActivity()));
        courseQuestionAdapter = new CourseQuestionAdapter();
        recyclerView.setAdapter(courseQuestionAdapter);
        contentView.findViewById(R.id.iv_close).setOnClickListener(new onCloseClickListener());
        contentView.findViewById(R.id.tv_input).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //弹出评论输入框
                questionPresenter.showQuestionInputDialog(getChildFragmentManager(), courseBean.ID);
            }
        });
    }


    @Override
    protected int getLayoutId() {
        return R.layout.dialog_course_detail_question;
    }

    @Override
    public void showWaitDialog(String message) {

    }

    @Override
    public void hideWaitDialog() {

    }

    @Override
    public void showToast(String message) {

    }

    @Override
    public Snackbar showSnackBar(View view, String content, int type) {
        return null;
    }

    @Override
    public void onDestroy() {
        if (questionPresenter != null){
            questionPresenter.detachView();
            questionPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void configCourseQuestionListData(List<CourseQuestionBean> courseQuestionBeanList) {
        courseQuestionAdapter.setNewData(courseQuestionBeanList);
    }

    @Override
    public void submitQuestionCallback(boolean isSuccess) {

    }

    public static class Builder extends BaseDialogBuilder<Builder, CourseDetailQuestionDialog> {

        private CourseBean courseBean;

        public Builder setCourseBean(CourseBean courseBean) {
            this.courseBean = courseBean;
            return this;
        }

        @Override
        public CourseDetailQuestionDialog build() {
            return CourseDetailQuestionDialog.newInstance(this);
        }
    }
}